package cn.iune.erp.purchase;


import java.sql.Date;
import java.text.ParseException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;


@Transactional
@Service
public class PurchaseOrderDetailServiceImpl implements IPurchaseOrderDetailService {

	@Autowired
	private IPurchaseOrderDetailDao dao;
	

	@Override
	public List<PurchaseOrderDetail> getAll(){
		return dao.getAll();
	}
	@Override
	public PurchaseOrderDetail getByID(String fID) {
		return dao.getByID(fID);
	}

	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}

	@Override
	public PurchaseOrderDetail getByNo(String stockNo, String serialNo, String enterpriseNo) {
		return dao.getByNo(stockNo, serialNo, enterpriseNo);
	}
	
	@Override
	public List<PurchaseOrderDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}

	
	///////////////////////业务功能///////////////////////////////////////

	
	@Override
	public List<PurchaseOrderDetail> getByStorageNo(String storageNo, String factoryNo, String enterpriseNo) {
		return dao.getByStorageNo(storageNo, factoryNo, enterpriseNo);
	}
	
	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException {
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
//		List<ShopCart> list = rowsToList(table.getRows());
		
		//System.out.println(table.getTableName());
		
		JSONObject json = new JSONObject();
		json.put("concept", "PurchaseOrderDetail");
		JSONArray rows = new JSONArray();
		
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			PurchaseOrderDetail obj = JSON.toJavaObject(javaObject, PurchaseOrderDetail.class);
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				PurchaseOrderDetail data = dao.update(obj);
				JSONObject jsonRow = new JSONObject();
				jsonRow.put("id", obj.getfID());
				jsonRow.put("version", data.getVersion());
				rows.add(jsonRow);
			}
		}
		
		json.put("rows", rows);
		
		return json;
	}
	
	@Transactional
	@Override
	public int confirm(String fID) {
		PurchaseOrderDetail sod = dao.getByID(fID);
		return this.confirm(sod);
	}
	
	@Transactional
	@Override
	public int active(String fID) {
		PurchaseOrderDetail sod = dao.getByID(fID);
		return this.active(sod);
	}
	
	@Transactional
	@Override
	public int confirm(PurchaseOrderDetail sod) {
		sod.setState("confirm");
		sod.setResultStatus("审核中");
		dao.update(sod);
		return 0;
	}
	
	@Transactional
	@Override
	public int active(PurchaseOrderDetail sod) {
		sod.setState("active");
		sod.setResultStatus("待审核");
		dao.update(sod);
		return 0;
	}

	@Transactional
	@Override
	public int confirmByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod:sods){
			this.confirm(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int activeByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod:sods){
			this.active(sod);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int open(String fID) {
		PurchaseOrderDetail sod = dao.getByID(fID);
		return this.open(sod);
	}
	
	@Transactional
	@Override
	public int close(String fID) {
		PurchaseOrderDetail sod = dao.getByID(fID);
		return this.close(sod);
	}
	
	@Transactional
	@Override
	public int open(PurchaseOrderDetail sod) {
		sod.setStatus("open");
		dao.update(sod);
		return 0;
	}
	
	@Transactional
	@Override
	public int close(PurchaseOrderDetail sod) {
		sod.setStatus("close");
		dao.update(sod);
		return 0;
	}

	@Transactional
	@Override
	public int openByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod:sods){
			this.open(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int closeByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod:sods){
			this.close(sod);
		}
		return 0;
	}
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(PurchaseOrderDetail s) {
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		s.setAudit("confirm");
		s.setConfirmDate(Utils.getCurrentDate());
		
		s.setResultStatus("已审核");
		dao.update(s);
		
		//执行更新操作
		/*
		int updateResult = this.doIt(s);
		if (updateResult<0){
			return -1;
		}
		*/
		return 0;
	}
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(String fID,Date confirmDate) {
		PurchaseOrderDetail s = dao.getByID(fID);
		return this.agreeIt(s);
	}

	//审核拒绝，改变审核状态为active
	@Override
	public int unAgree(PurchaseOrderDetail s) {
		//更新数据库工具入库单的单据状态
		s.setAudit("active");
		s.setConfirmDate(null);
		
		s.setResultStatus("审核中");
		dao.update(s);
		
		//执行更新操作
		/*
		int updateResult = this.unDo(s);
		if (updateResult<0){
			return -1;
		}
		*/
		return 0;
	}
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(String fID) {
		PurchaseOrderDetail s = dao.getByID(fID);
		return this.unAgree(s);
	}

	@Transactional
	@Override
	public int agreeItByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod :sods){
			this.agreeIt(sod);
		}
		return 0;
	}
	@Transactional
	@Override
	public int unAgreeByMasterID(String fMasterID){
		List<PurchaseOrderDetail> sods = dao.getByMasterID(fMasterID);
		for(PurchaseOrderDetail sod :sods){
			this.unAgree(sod);
		}
		return 0;
	}


}